import 'package:flutter/material.dart';
import 'package:life_milestones/controller/MS-global-user-controller.dart';
import 'package:life_milestones/util/MS-database-util.dart';
import 'dart:math';
import '../MS-Model/MS-home-model.dart';

class MSHomeController extends ChangeNotifier{
   List<MSCategory> _categories = [];

   MSHomeController(){
     initTaskList();
     refreshCategory();
   }
   List<MSCategory> get categories => _categories;
   set categories(List<MSCategory> value){
     _categories = value;
     notifyListeners();
   }
   MSCategory? getCategoryById(int id){
     var list = _categories.where((element) => element.id == id).toList();
     return list.isNotEmpty ? list[0]:null;
   }

   refreshCategory()async {
     var value = await MSDatabaseUtil().query(
         "Category", ["id", "name", "userId", "time"], ["userId"],
         [MSGlobalUserController().user!.userId!]);
     List<MSCategory> list = [];
     for(int index = 0;index < (value?.length ?? 0);index++){
       var e = value![index];
       var temp = MSCategory.fromJson(e);
       var items = await MSDatabaseUtil().query("Task", ["id","name","userId","time","isFinished","categoryId"], ["categoryId"],[temp.id!]);
       temp.items = items?.map((e) => MSItem.fromJson(e)).toList() ?? [];
       list.add(temp);
     }
     categories = list;

   }

   addCategory(String name)async{
      await MSDatabaseUtil().insert("Category", {"name":name,"time":"0","userId":MSGlobalUserController().user!.userId!});
      await refreshCategory();
   }

   updateCategory(MSCategory category)async{
     await MSDatabaseUtil().update("Category", {"name":category.name,"time":category.time,"userId":category.userId,"id":category.id},"id",[category.id]);
     await refreshCategory();
   }

   deleteCategory(MSCategory category)async{
     await MSDatabaseUtil().delete("Category",["id"],[category.id]);
     await MSDatabaseUtil().delete("Task", ["categoryId"], [category.id]);
     await refreshCategory();
   }

   addTask(MSItem item)async{
     await MSDatabaseUtil().insert("Task", {"name":item.name,"time":item.time,"isFinished":item.isFinished,"userId":item.userId,"categoryId":item.categoryId});
     refreshCategory();
   }

   updateTask(MSItem item)async{
     await MSDatabaseUtil().update("Task",  {"name":item.name,"time":item.time,"isFinished":item.isFinished,"userId":item.userId,"categoryId":item.categoryId},"id",[item.id]);
     refreshCategory();
   }

   deleteTask(MSItem item)async{
     await MSDatabaseUtil().delete("Task",["id"],[item.id]);
     refreshCategory();
   }

   Future<void> deleteAllTask(MSCategory category)async{
     await MSDatabaseUtil().delete("Task",["categoryId"],[category.id]);
     refreshCategory();
   }
   deleteAllFinishedTask(MSCategory category)async{
     await MSDatabaseUtil().delete("Task",["categoryId","isFinished"],[category.id,1]);
     refreshCategory();
   }

   onLogout()async{
     categories = [];
   }

  Future<void> onDelete()async{
     categories = [];
     await MSDatabaseUtil().delete("Task",["userId"],[MSGlobalUserController().user!.userId]);
     await MSDatabaseUtil().delete("Category",["userId"],[MSGlobalUserController().user!.userId]);
   }

   initTaskList()async{
     if(MSGlobalUserController().checkIsFirstLogin()){
       var id = await MSDatabaseUtil().insert("Category", {"name":"Jeo 's todolist","time":"${Random().nextInt(24)}:00","userId":MSGlobalUserController().user!.userId!});
       await MSDatabaseUtil().insert("Task",  {"name":"Organise school bags.","time":"${Random().nextInt(24)}:00","isFinished":0,"userId":MSGlobalUserController().user!.userId!,"categoryId":id});
       await MSDatabaseUtil().insert("Task",  {"name":"Eating dinner.","time":"${Random().nextInt(24)}:00","isFinished":0,"userId":MSGlobalUserController().user!.userId!,"categoryId":id});
       await MSDatabaseUtil().insert("Task",  {"name":"Integral bed.","time":"${Random().nextInt(24)}:00","isFinished":0,"userId":MSGlobalUserController().user!.userId!,"categoryId":id});

       id = await MSDatabaseUtil().insert("Category", {"name":"Liri's Reminders","time":"${Random().nextInt(24)}:00","userId":MSGlobalUserController().user!.userId!});
       await MSDatabaseUtil().insert("Task",  {"name":"Bring an umbrella when you go out.","time":"${Random().nextInt(24)}:00","isFinished":0,"userId":MSGlobalUserController().user!.userId!,"categoryId":id});
       await MSDatabaseUtil().insert("Task",  {"name":"Bring a glass of water.","time":"${Random().nextInt(24)}:00","isFinished":0,"userId":MSGlobalUserController().user!.userId!,"categoryId":id});
       await MSDatabaseUtil().insert("Task",  {"name":"Wear thick clothes.","time":"${Random().nextInt(24)}:00","isFinished":0,"userId":MSGlobalUserController().user!.userId!,"categoryId":id});
       MSGlobalUserController().setIsFirstLogin();
       await refreshCategory();
     }

   }


}